home *** CD-ROM | disk | FTP | other *** search
/ PC Open 97 / PC Open 97 CD2.bin / Demo / FileMaker / Data1.cab / complex_table.xsl4 < prev    next >
Encoding:
Extensible Markup Language  |  2002-05-24  |  8.0 KB  |  240 lines

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fmp="http://www.filemaker.com/fmpxmlresult" exclude-result-prefixes="fmp">
  3.     <xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/>
  4.     <!--
  5. File: complex_table.xsl
  6.  
  7. Transforms data exported in the FMPXMLRESULT grammar into an 
  8. HTML table. Variables are provided to customize the color of 
  9. headings and odd/even rows.
  10.  
  11. ===============================================================
  12.  
  13. Copyright ┬⌐ 2002 FileMaker, Inc.
  14. All rights reserved.
  15.  
  16. Redistribution and use in source and binary forms, with or
  17. without modification, are permitted provided that the following
  18. conditions are met:
  19.  
  20. * Redistributions of source code must retain the above copyright
  21.   notice, this list of conditions and the following disclaimer.
  22.  
  23. * Redistributions in binary form must reproduce the above copyright
  24.   notice, this list of conditions and the following disclaimer in 
  25.   the documentation and/or other materials provided with the
  26.   distribution.
  27.  
  28. * Neither the name of the FileMaker, Inc. nor the names of its 
  29.   contributors may be used to endorse or promote products derived
  30.   from this software without specific prior written
  31.   permission.
  32.  
  33. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  34. CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  35. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  36. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
  37. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 
  38. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
  39. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  40. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  41. BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  42. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  43. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  44. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45.     
  46. ===============================================================
  47. -->
  48.     <!--
  49. A slightly more involved stylesheet that builds an HTML table using multiple 
  50. templates and more sophisticated select statements.
  51. -->
  52.     <xsl:template match="fmp:FMPXMLRESULT">
  53.         <html>
  54.             <head>
  55.                 <title>
  56.                     <xsl:value-of select="$dbName"/>
  57.                 </title>
  58.             </head>
  59.             <body>
  60.                 <table border="0" cellPadding="3" cellSpacing="1">
  61.                     <xsl:call-template name="header"/>
  62.                     <!--
  63. This next block simply alternates the colors like this:
  64.                     
  65. MainOne | ColorOne
  66. MainTwo | ColorTwo
  67. MainOne | ColorOne
  68. MainTwo | ColorTwo
  69.                                   
  70. using the position() mod 2, checking for odd or
  71. even rows.
  72. -->
  73.                     <xsl:for-each select="fmp:RESULTSET/fmp:ROW">
  74.                         <xsl:variable name="id-background-color">
  75.                             <xsl:choose>
  76.                                 <xsl:when test="position() mod 2 = 1">
  77.                                     <xsl:value-of select="$mainOne"/>
  78.                                 </xsl:when>
  79.                                 <xsl:when test="position() mod 2 = 0">
  80.                                     <xsl:value-of select="$mainTwo"/>
  81.                                 </xsl:when>
  82.                             </xsl:choose>
  83.                         </xsl:variable>
  84.                         <xsl:variable name="data-background-color">
  85.                             <xsl:choose>
  86.                                 <xsl:when test="position() mod 2 = 1">
  87.                                     <xsl:value-of select="$colorOne"/>
  88.                                 </xsl:when>
  89.                                 <xsl:when test="position() mod 2 = 0">
  90.                                     <xsl:value-of select="$colorTwo"/>
  91.                                 </xsl:when>
  92.                             </xsl:choose>
  93.                         </xsl:variable>
  94.                         <tr>
  95.                             <xsl:for-each select="fmp:COL">
  96.                                 <xsl:variable name="background-color">
  97.                                     <xsl:choose>
  98.                                         <xsl:when test="position() = 1">
  99.                                             <xsl:value-of select="$id-background-color"/>
  100.                                         </xsl:when>
  101.                                         <xsl:otherwise>
  102.                                             <xsl:value-of select="$data-background-color"/>
  103.                                         </xsl:otherwise>
  104.                                     </xsl:choose>
  105.                                 </xsl:variable>
  106.                                 <td bgcolor="{$background-color}">
  107.                                     <xsl:value-of select="fmp:DATA"/>
  108.                                 </td>
  109.                             </xsl:for-each>
  110.                         </tr>
  111.                     </xsl:for-each>
  112.                     <xsl:call-template name="footer"/>
  113.                 </table>
  114.             </body>
  115.         </html>
  116.     </xsl:template>
  117.     <!--
  118. Template: header
  119.             
  120. Creates the heading for the HTML table.
  121.             
  122. The database name and number of records are displayed above the fields names.
  123. Field names are displayed in "headerFontColor" color with a cell background color
  124. of "headerColor".
  125.     -->
  126.     <xsl:template name="header">
  127.         <tr>
  128.             <td align="middle">
  129.                 <xsl:attribute name="bgColor"><xsl:value-of select="$headerColor"/></xsl:attribute>
  130.                 <xsl:attribute name="colspan"><xsl:call-template name="numfields"/></xsl:attribute>
  131.                 <font size="+1">
  132.                     <xsl:attribute name="color"><xsl:value-of select="$headerFontColor"/></xsl:attribute>
  133.                     <xsl:text>Database: </xsl:text>
  134.                     <xsl:value-of select="fmp:DATABASE/@NAME"/>
  135.                 </font>
  136.             </td>
  137.         </tr>
  138.         <tr>
  139.             <td align="middle">
  140.                 <xsl:attribute name="bgColor"><xsl:value-of select="$headerColor"/></xsl:attribute>
  141.                 <xsl:attribute name="colspan"><xsl:call-template name="numfields"/></xsl:attribute>
  142.                 <font color="#ffffff" size="+1">
  143.                     <xsl:text>Records: </xsl:text>
  144.                     <xsl:value-of select="fmp:DATABASE/@RECORDS"/>
  145.                 </font>
  146.             </td>
  147.         </tr>
  148.         <tr>
  149.             <xsl:for-each select="fmp:METADATA/fmp:FIELD">
  150.                 <td align="middle">
  151.                     <xsl:attribute name="bgColor"><xsl:value-of select="$headerColor"/></xsl:attribute>
  152.                     <font size="+1">
  153.                         <xsl:attribute name="color"><xsl:value-of select="$headerFontColor"/></xsl:attribute>
  154.                         <xsl:value-of select="@NAME"/>
  155.                     </font>
  156.                 </td>
  157.             </xsl:for-each>
  158.         </tr>
  159.     </xsl:template>
  160.     <!--
  161. Template: footer
  162.             
  163. Creates the footer for the HTML table with a cell background color of "footerColor".
  164.             
  165. The version of FileMaker Pro used to create the XML file is displayed along with the 
  166. build date.
  167.     -->
  168.     <xsl:template name="footer">
  169.         <tr>
  170.             <td align="middle">
  171.                 <xsl:attribute name="bgColor"><xsl:value-of select="$footerColor"/></xsl:attribute>
  172.                 <xsl:attribute name="colspan"><xsl:call-template name="numfields"/></xsl:attribute>
  173.                 <font color="#ffffff" size="+1">
  174.                     <xsl:text>FileMaker Pro Version: </xsl:text>
  175.                     <xsl:value-of select="fmp:PRODUCT/@VERSION"/>
  176.                 </font>
  177.             </td>
  178.         </tr>
  179.         <tr>
  180.             <td align="middle">
  181.                 <xsl:attribute name="bgColor"><xsl:value-of select="$footerColor"/></xsl:attribute>
  182.                 <xsl:attribute name="colspan"><xsl:call-template name="numfields"/></xsl:attribute>
  183.                 <font size="+1">
  184.                     <xsl:attribute name="color"><xsl:value-of select="$headerFontColor"/></xsl:attribute>
  185.                     <xsl:text>Build: </xsl:text>
  186.                     <xsl:value-of select="fmp:PRODUCT/@BUILD"/>
  187.                 </font>
  188.             </td>
  189.         </tr>
  190.     </xsl:template>
  191.     <!--
  192. Template: numfields
  193.  
  194. Simple utility template that checks for the number of children in the METADATA
  195. element and returns the number of fields in the database - handy when building
  196. tables.
  197. -->
  198.     <xsl:template name="numfields" match="fmp:METADATA">
  199.         <xsl:value-of select="count(fmp:METADATA/child::*)"/>
  200.     </xsl:template>
  201.     <xsl:variable name="dbName">
  202.         <xsl:value-of select="fmp:FMPXMLRESULT/fmp:DATABASE/@NAME"/>
  203.     </xsl:variable>
  204.     <!--
  205. The table is arranged like this:
  206.          
  207. HeaderColor
  208. MainOne | ColorOne
  209. MainTwo | ColorTwo
  210. MainOne | ColorOne
  211. MainTwo | ColorTwo
  212. FooterColor
  213.       
  214. Changing the values of the variables below will allow for easy modifications to the
  215. appearance of the resulting HTML table without the need for editing the XSL
  216. templates. 
  217.     -->
  218.     <xsl:variable name="mainOne">
  219.         <xsl:text>#9bb0d4</xsl:text>
  220.     </xsl:variable>
  221.     <xsl:variable name="mainTwo">
  222.         <xsl:text>#7f95d4</xsl:text>
  223.     </xsl:variable>
  224.     <xsl:variable name="colorOne">
  225.         <xsl:text>#bcd0f0</xsl:text>
  226.     </xsl:variable>
  227.     <xsl:variable name="colorTwo">
  228.         <xsl:text>#9cb0f0</xsl:text>
  229.     </xsl:variable>
  230.     <xsl:variable name="headerColor">
  231.         <xsl:text>#000000</xsl:text>
  232.     </xsl:variable>
  233.     <xsl:variable name="footerColor">
  234.         <xsl:text>#000000</xsl:text>
  235.     </xsl:variable>
  236.     <xsl:variable name="headerFontColor">
  237.         <xsl:text>#ffffff</xsl:text>
  238.     </xsl:variable>
  239. </xsl:stylesheet>
  240.